/*
 * 8.4.cpp 1222_poj
 *
 *  Created on: 2012-5-21
 *      Author: jawinton
 */

#include <iostream>
#include <cstring>
#include <cstdio>

int puzzle[7][8], result[7][8];

void press (int i, int j) {
	result[i][j] = 1;
	puzzle[i][j] = 1-puzzle[i][j];
	puzzle[i+1][j] = 1-puzzle[i+1][j];
	puzzle[i-1][j] = 1-puzzle[i-1][j];
	puzzle[i][j+1] = 1-puzzle[i][j+1];
	puzzle[i][j-1] = 1-puzzle[i][j-1];
}

bool isSuccess () {
	for (int i=1; i<5; i++) {
		for (int j=1; j<=6; j++) {
			if (puzzle[i][j] == 1)
				press(i+1, j);
		}
	}
	for (int i=1; i<=6; i++) {
		if (puzzle[5][i] == 1) return false;
	}
	return true;
}

void enumate (int j) {
	for (int i=6; i>=1; i--) {
		int tmp = 1;
		for (int k=1; k<i; k++)
			tmp *=2;
		if (j/tmp == 1) {
			press(1, i);
			j -= tmp;
		}
	}
}

int main (void) {
	int n;
	scanf("%d", &n);
	for (int i=1; i<=n; i++) {
		int tmp[7][8];
		for (int j=1; j<=5; j++) {
			for (int k=1; k<=6; k++) {
				scanf("%d", &puzzle[j][k]);
				tmp[j][k] = puzzle[j][k];
				result[j][k] = 0;
			}
		}
		for (int j=0; j<64; j++) {
			enumate(j);
			if (isSuccess())
				break;
			else {
				for (int k=1; k<=5; k++) {
					for (int l=1; l<=6; l++){
						result[k][l] = 0;
						puzzle[k][l] = tmp[k][l];
					}
				}
			}
		}
		printf("PUZZLE #%d\n", i);
		for (int j=1; j<=5; j++) {
			for (int k=1; k<=6; k++){
				printf("%d", result[j][k]);
				if (k!=6)
					printf(" ");
				else
					printf("\n");
			}
		}
	}
	return 0;
}
